import time
from app import server_start
import sys


def parse_args():
    args={}
    for i,arg in enumerate(sys.argv):
        if arg.startswith('--') and i<len(sys.argv):
            args[arg.removeprefix('--')]=sys.argv[i+1]
    return args

def parse_bool(data:str):
    if isinstance(data,bool):
        return data
    if isinstance(data,str) ==False:
        raise ValueError(f'{data} is invalid data type!')
    if data.strip().lower()=='false':
        return False
    if data.strip().lower()=='true':
        return True
    raise ValueError(f'Can not parse {data} to boolean value!')

def main():
    args=parse_args()
    port=int(args.get('port',8888))
    open_browser=parse_bool(args.get('open-browser',True))


    

    from multiprocessing import Process
    p=Process(target=server_start,args=(port,))
    p.start()
    print('server is running...')
    if open_browser:
        time.sleep(1)
        import webbrowser
        webbrowser.open('http://127.0.0.1:'+str(port))
    p.join()

if __name__ == '__main__':
    main()